import random

from .base_layer import BaseLayer
from . import utils

FULL_RANDOM = False  # 通过此变量来确定信息战是完全随机，还是有一点策略性


class EncMsgTransferLayer(BaseLayer):
    def __init__(self):
        self.color = None

    def setup(self, color):
        self.color = color

    def step(self):
        action_list = None
        if FULL_RANDOM:
            action_list = self.random_strategy()
        else:
            action_list = self.with_brain_strategy()
        return action_list

    def random_strategy(self):
        """
        完全随机的作战风格
        """
        action_list = []
        has_choose = {
            # 101:False,
            # 102:False,
            103: False,
            104: False
        }
        # 循环执行0～4次
        for _ in range(random.randint(0, 4)):
            # 随机选择一个动作进行尝试生成
            random_number = random.randint(1, 4)
            if random_number == 1:
                # 1、尝试进行101-无线通信
                pass
            elif random_number == 2:
                # 2、尝试进行102-人工通报
                pass
            elif random_number == 3:
                # 3、尝试进行103-网络重建
                if has_choose[103] == True:
                    continue
                action = self.gen_network_rebuild()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[103] = True
            elif random_number == 4:
                # 4、尝试进行104-通信信道切换
                if has_choose[104] == True:
                    continue
                action = self.gen_communicate_channel_change()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[104] = True
        return action_list

    def with_brain_strategy(self):
        """
        带点脑子的作战风格
        """
        action_list = []
        # 2、人工通报：当通信质量低于阈值（0.2 - 0.3）或通信中断时采用，暂略
        # 3、网络重建： 上一时刻有算子通信中断
        if utils.get_last_step_communication_stop()["red" if self.color == 0 else "blue"] == True:
            action = self.gen_network_rebuild()
            if action and utils.action_valid(action):
                action_list.append(action)
        # 4、通信信道切换：上一时刻通信质量低于阈值（0.7）
        if utils.get_communication_quality()["red" if self.color == 0 else "blue"] <= 0.7:
            action = self.gen_communicate_channel_change()
            if action and utils.action_valid(action):
                action_list.append(action)
        return action_list

    def gen_network_rebuild(self):
        # 1、获取我方通信质量不是最佳且未死亡算子的id构成的列表
        has_network_error_ids = []
        ops_status = utils.get_ops_status(self.color)
        network_status = utils.get_network_status(self.color)
        for op_id, op_status in network_status.items():
            if op_status < 1 and ops_status[op_id]["live_status"] == True:
                has_network_error_ids.append(op_id)
        # 2、随机取出一个活着的算子id作为目标对象，如果算子都死亡了则直接返回
        if len(has_network_error_ids) == 0:
            return None
        ops_id = random.choice(has_network_error_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 103,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[103]
        }
        return action

    def gen_communicate_channel_change(self):
        """
        尝试生成通信信道切换动作
        """
        # 生成动作
        action = {
            "color": self.color,
            "type": 104,
            "remain_time": utils.ACTION_COST_TIME[104]
        }
        return action

    def reset(self):
        pass
